package com.xzmzhp.dao.power;

import com.xzmzhp.pojo.power.Competence;
import com.xzmzhp.pojo.power.RequestPath;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author 王源(xm)
 * @Date: 2020/11/15 18:36
 * @Description: 权限信息表操作类
 */

@Repository
public interface CompetenceMapper {

    /**
     * 查询权限信息表所有数据
     *
     * @return Competence类型的list集合
     */
    @Select("select competence_id,competence_name from competence")
    List<Competence> findAll();

    /**
     * 根据competenceId查询数据
     *
     * @param competenceId 权限信息表id
     * @return Competence对象
     */
    @Select("select competence_id,competence_name from competence where competence_id=#{competenceId}")
    Competence findById(@Param("competenceId") int competenceId);

    /**
     * 根据权限名称查询数据
     *
     * @param competenceName 权限名称
     * @return Competence对象
     */
    @Select("select competence_id,competence_name from competence where competence_name=#{competenceName}")
    Competence findByName(@Param("competenceName") String competenceName);

    /**
     * 根据权限id查询所有路径
     *
     * @param competenceId 权限id
     * @return 所有请求路径
     */
    @Select("SELECT request_path.url, request_path.description" +
            "FROM request_path " +
            "WHERE request_path.id IN " +
            "( " +
            "SELECT menu_request.id " +
            "FROM menu_request, competence_menu " +
            "WHERE menu_request.menu_id = competence_menu.competence_menu_id " +
            "AND competence_menu.competence_id = #{competenceId}" +
            ")")
    List<RequestPath> findByCompetenceId(@Param("competenceId") int competenceId);

    /**
     * 添加权限
     *
     * @param competenceName 权限名称
     * @return Integer
     */
    @Insert("insert into competence values(null,#{competenceName},null,null)")
    Integer addCompetence(@Param("competenceName") String competenceName);

    /**
     * 根据id修改权限
     *
     * @param competence 对象
     * @return Integer
     */
    @Update("update competence set competence_name=#{competenceName} where competence_id=#{competenceId}")
    Integer updateCompetence(Competence competence);

    /**
     * 根据id删除权限信息
     *
     * @param competenceId 权限id
     * @return Integer
     */
    @Delete("delete from competence where competence_id=#{competenceId}")
    Integer deleteCompetence(@Param("competenceId") int competenceId);
}
